// This file define the CDAPString classes used for both linux and windows os
#ifndef _DAP_STRING_H_
#define _DAP_STRING_H_
#ifndef _LINUX_OS_
 #include "AfxTempl.h"
#else
#define AFXAPI
#include "w_stl.h"				//20030820
#define AFX_EXT_CLASS			//20030820
using namespace std;			//20030820
#endif
class AFX_EXT_CLASS CDAPString
{
public:
// Constructors
	// constructs empty CDAPString
	CDAPString();
	// copy constructor
	CDAPString(const CDAPString& stringSrc);
#ifndef _LINUX_OS_
	CDAPString(const CString& stringSrc);
#endif
	// from a single character
	CDAPString(char ch, int nRepeat = 1);
	// from an ANSI string (converts to TCHAR)
	CDAPString(char * lpsz);
	// from unsigned characters
	CDAPString(const char* psz);
// Operator
public:
	operator const char*() const{
		return (const char*)m_pchData;
	}
// Attributes & Operations

	// get data length
	int GetLength() const;
	// TRUE if zero length
	bool IsEmpty() const;
	// clear contents to empty
	void RemoveAll();

	// return single character at zero-based index
	char GetAt(int nIndex) const;
	// return single character at zero-based index
	char operator[](int nIndex) const;
	// set a single character at zero-based index
	void SetAt(int nIndex, char ch);

	// overloaded assignment

	// ref-counted copy from another CDAPString
	const CDAPString& operator=(const CDAPString& stringSrc);
#ifndef _LINUX_OS_
	const CDAPString& operator=(const CString& stringSrc);
#endif
	// set string content to single character
	const CDAPString& operator=(char ch);
	// copy string content from ANSI string (converts to TCHAR)
	const CDAPString& operator=(const char* lpsz);
	// copy string content from unsigned chars
	const CDAPString& operator=(const unsigned char* psz);

	// string concatenation

	// concatenate from another CDAPString
	const CDAPString& operator+=(const CDAPString& string);

	// concatenate a single character
	const CDAPString& operator+=(char ch);

	friend CDAPString  AFXAPI operator+(const CDAPString& string1,
			const CDAPString& string2);
	friend CDAPString  AFXAPI operator+(const CDAPString& string, char ch);
	friend CDAPString  AFXAPI operator+(char ch, const CDAPString& string);

	// string comparison

	// straight character comparison
	int Compare(const char* lpsz) const;
	// compare ignoring case
	int CompareNoCase(const char* lpsz) const;

	// simple sub-string extraction

	// return nCount characters starting at zero-based nFirst
	CDAPString Mid(int nFirst, int nCount) const;
	// return all characters starting at zero-based nFirst
	CDAPString Mid(int nFirst) const;
	// return first nCount characters in string
	CDAPString Left(int nCount) const;
	// return nCount characters from end of string
	CDAPString Right(int nCount) const;


	// upper/lower/reverse conversion

	// NLS aware conversion to uppercase
	void MakeUpper();
	// NLS aware conversion to lowercase
	void MakeLower();

	// trimming whitespace (either side)

	// remove whitespace starting from right edge
	void TrimRight();
	// remove whitespace starting from left side
	void TrimLeft();

	// trimming anything (either side)

	// remove continuous occurrences of chTarget starting from right
	void TrimRight(char chTarget);
	// remove continuous occurrences of chTarget starting from left
	void TrimLeft(char chTarget);

	// advanced manipulation

	// replace occurrences of chOld with chNew
	int Replace(char chOld, char chNew);
	// replace occurrences of substring lpszOld with lpszNew;
	// empty lpszNew removes instances of lpszOld
	int Replace(const char* lpszOld, const char* lpszNew);
	// remove occurrences of chRemove
	int Remove(char chRemove);
	// insert character at zero-based index; concatenates
	// if index is past end of string
	int Insert(int nIndex, char ch);
	// insert substring at zero-based index; concatenates
	// if index is past end of string
	int Insert(int nIndex, const char* pstr);
	// delete nCount characters starting at zero-based index
	int Delete(int nIndex, int nCount = 1);

	// searching

	// find character starting at left, -1 if not found
	int Find(char ch) const;
	// find character starting at right
	int ReverseFind(char ch) const;
	// find character starting at zero-based index and going right
	int Find(char ch, int nStart) const;
	// find first instance of any character in passed string
	int FindOneOf(const char * lpszCharSet) const;
	// find first instance of substring
	int Find(const char * lpszSub) const;
	// find first instance of substring starting at zero-based index
	int Find(const char * lpszSub, int nStart) const;

	// simple formatting

	// printf-like formatting using passed string
#ifndef _LINUX_OS_
	void _cdecl Format(const char * lpszFormat, ...);
#else
	void  Format(const char * lpszFormat, ...);					//20030820
#endif

	// Access to string implementation buffer as "C" character array

// Implementation
public:
	~CDAPString();
	int GetAllocLength() const{return m_nAllocLen;};
// Attribute
public:
	char* m_pchData;   // pointer to ref counted string data
	int	  m_nAllocLen;
// Other Helper
public:
	bool AllocBuffer(int nNewLen);
	bool IncreaseBuffer(int nIncCount);
	int  GetDataLength() const;
	void ConcatCopy(int nSrc1Len, const char* lpszSrc1Data,	int nSrc2Len, const char *lpszSrc2Data);
	char* GetBuffer(int nLen=0);

// Static function
public:
	static int GetSafeStrLen(const char* nChar);
};
class AFX_EXT_CLASS CDAPStringArray : public CArray<CDAPString, CDAPString &>
{
};
inline CDAPString AFXAPI operator+(const CDAPString& string1, char ch)
{
	CDAPString s;
	s.ConcatCopy(string1.GetLength(), string1.m_pchData, 1, &ch);
	return s;
}

inline CDAPString AFXAPI operator+(char ch, const CDAPString& string)
{
	CDAPString s;
	s.ConcatCopy(1, &ch, string.GetLength(), string.m_pchData);
	return s;
}
inline CDAPString AFXAPI operator+(const CDAPString& string1, const CDAPString& string2)
{
	CDAPString s;
	s.ConcatCopy(string1.GetLength(), string1.m_pchData,
		string2.GetLength(), string2.m_pchData);
	return s;
}

inline CDAPString AFXAPI operator+(const CDAPString& string, const char* lpsz)
{
	CDAPString s;
	if(lpsz==0)
		return string;
	s.ConcatCopy(string.GetLength(), string.m_pchData,
		strlen(lpsz), lpsz);
	return s;
}

inline CDAPString AFXAPI operator+(const char* lpsz, const CDAPString& string)
{
	CDAPString s;
	if(lpsz==0)
		return string;
	s.ConcatCopy(strlen(lpsz), lpsz, string.GetLength(),
		string.m_pchData);
	return s;
}
extern short ParseStringEx(CDAPString strBuffer, CDAPStringArray &arrString, char Delimer);

#endif